package com.ztz.mynewdemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ztz.mynewdemo.R;
import com.ztz.mynewdemo.adapter.SocietyAdapter;
import com.ztz.mynewdemo.bean.SocietyResponse;
import com.ztz.mynewdemo.utils.Constants;
import com.ztz.mynewdemo.utils.NewsApi;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wqewqe on 2017/5/18.
 */

public class SocietyFragment extends BaseFragment {
    SmartRefreshLayout refreshLayout;
    RecyclerView socRec;
    FloatingActionButton socIv;
    SocietyAdapter adapter;
    List<SocietyResponse.ResultBean.DataBean> beanList=new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.society_fragment,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        refreshLayout=(SmartRefreshLayout) view.findViewById(R.id.soc_smart);
        setRefreshLayout();
        socRec=(RecyclerView)view.findViewById(R.id.soc_recycler);
        socIv=(FloatingActionButton) view.findViewById(R.id.top);
        socIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                socRec.smoothScrollToPosition(0);
            }
        });

        LinearLayoutManager manager=new LinearLayoutManager(getContext());
        socRec.setLayoutManager(manager);
        adapter=new SocietyAdapter(getContext(),beanList);
        socRec.setAdapter(adapter);
    }
    private void setRefreshLayout(){
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh(1500);
            setRefreshThemeColor();
            beanList.clear();
            getData();
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            refreshLayout.finishLoadMore(1500);
            getData();
        });
    }
    private int themeCount;
    private void setRefreshThemeColor() {
        themeCount++;
        if (themeCount % Constants.FOUR == Constants.ONE) {
            refreshLayout.setPrimaryColorsId(Constants.BLUE_THEME, R.color.white);
        } else if (themeCount % Constants.FOUR == Constants.TWO) {
            refreshLayout.setPrimaryColorsId(Constants.GREEN_THEME, R.color.white);
        } else if (themeCount % Constants.FOUR == Constants.THREE) {
            refreshLayout.setPrimaryColorsId(Constants.RED_THEME, R.color.white);
        } else if (themeCount % Constants.FOUR == Constants.ZERO) {
            refreshLayout.setPrimaryColorsId(Constants.ORANGE_THEME, R.color.white);
        }
    }

    @Override
    protected void getData() {
        OkGo.<String>get(NewsApi.HOST)
                .tag(this)
                .params("key","f0af46ff50a9fbc5522315f2b4601667")
                .params("type","shehui")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String s=response.body();
//                        Log.d("onSuccess", "onSuccess: ");
                        Gson gson=new Gson();
                        SocietyResponse socResponse=gson.fromJson(s,SocietyResponse.class);
                        beanList.addAll(socResponse.getResult().getData());
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                adapter.notifyDataSetChanged();
                            }
                        });
                    }
                });
    }
}
